package net.tailfish.starter.common.configure;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import net.tailfish.starter.common.generator.GeneratorProperties;
import net.tailfish.starter.common.generator.IdGeneratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @PackageName net.tailfish.starter.common.configure
 * @ClassName AutoGeneratorConfig
 * @Author tailFish
 * @Date Created in 14:07 2021/7/26
 * @Description ID生成 配置类
 */
@EnableConfigurationProperties(value = GeneratorProperties.class)
@SpringBootConfiguration
public class AutoGeneratorConfig {

    @Autowired
    private GeneratorProperties generatorProperties;

    @ConditionalOnMissingBean
    @Bean
    public Snowflake snowflake(){
        return IdUtil.getSnowflake(generatorProperties.getWorkerId(),generatorProperties.getDatacenterId());
    }

    @ConditionalOnMissingBean
    @Bean
    public IdGeneratorUtils idGeneratorUtils(Snowflake snowflake){
        IdGeneratorUtils idGeneratorUtils = new IdGeneratorUtils(snowflake);
        return idGeneratorUtils;
    }
}
